from django.shortcuts import render

# Create your views here.

from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import CreateModelMixin
from rest_framework.decorators import action
from users.models import Ops_user
from users.serializers import LoginModelSerializer
from utils.Ops_responses import OpsResponse
from utils.log_db import handle_log
from django.core.cache import cache


class LoginView(GenericViewSet):
    @action(methods=['POST'], detail=False)
    def login(self, request):
        ser = LoginModelSerializer(data=request.data)
        ser.is_valid(raise_exception=True)
        username = ser.context.get('username')
        username_id = ser.context.get('username_id')
        token = ser.context.get('token')
        icon = ser.context.get('icon')
        data = {'username': username, 'username_id': username_id, 'token': token, 'icon': icon}
        cache.set('username_id', username_id)
        handle_log('登录', '登录成功')
        return OpsResponse(data=data)
